import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:wu_you/data/cache/id_cache.dart';
import 'package:wu_you/data/repository/user_repository.dart';
import 'package:wu_you/l10n/messages.dart';
import 'package:wu_you/res/colors.dart';
import 'package:wu_you/utils/extension_utils.dart';
import 'package:wu_you/utils/utils.dart';

class InputDialog extends StatefulWidget{
  @override
  InputDialogState createState() => InputDialogState();
}
class InputDialogState extends State<InputDialog>{
  final _controller1 = TextEditingController();
  final _controller2 = TextEditingController();
  var message;
  @override
  Widget build(BuildContext context) {
    message = AppLocalizations.of(context);
    return Material(
      color: Colors.transparent,
      child: WillPopScope(
        child: Center(
          child: Padding(
              padding: EdgeInsets.only(left: 40, right: 40),
              child: Container(
                  margin: EdgeInsets.only(bottom: 8),
                  decoration: BoxDecoration(
                      borderRadius: BorderRadius.all(Radius.circular(8)),
                      color: Colors.white
                  ),
                  child: Column(
                    mainAxisSize: MainAxisSize.min,
                    children: [
                      Container(
                          margin: EdgeInsets.only(top: 15),
                          child: Text("${message['text36']}",style: TextStyle(fontSize: 16,fontWeight: FontWeight.bold),)
                      ),
                      Container(
                        margin: EdgeInsets.only(top: 15),
                        padding: EdgeInsets.only(left: 20,right: 20),
                        child: TextField(
                            controller: _controller1,
                            style: TextStyle(fontSize: 14, color: AppColors.textColor),
                            // maxLength: 11,
                            keyboardType: TextInputType.name,
                            decoration: InputDecoration(
                              fillColor: Colors.white,
                              filled: true,
                              border: UnderlineInputBorder(
                                borderSide: BorderSide.none,
                                // borderRadius: const BorderRadius.all(Radius.circular(24)),
                              ),
                              counterText: '',
                              hintText: "${message['text37']}",
                              hintStyle: TextStyle(
                                fontSize: 14,
                                color: AppColors.textHintColor,
                              ),
                              suffixIconConstraints: BoxConstraints.loose(Size(100, 56)),
                            )
                        ),
                      ),
                      Divider(),
                      Container(
                        padding: EdgeInsets.only(left: 20,right: 20),
                        child: TextField(
                            controller: _controller2,
                            style: TextStyle(fontSize: 14, color: AppColors.textColor),
                            // maxLength: 11,
                            keyboardType: TextInputType.phone,
                            decoration: InputDecoration(
                              fillColor: Colors.white,
                              filled: true,
                              border: UnderlineInputBorder(
                                borderSide: BorderSide.none,
                                // borderRadius: const BorderRadius.all(Radius.circular(24)),
                              ),
                              counterText: '',
                              hintText: "${message['text38']}",
                              hintStyle: TextStyle(
                                fontSize: 14,
                                color: AppColors.textHintColor,
                              ),
                              suffixIconConstraints: BoxConstraints.loose(Size(100, 56)),
                            )
                        ),
                      ),
                      SizedBox(height: 6,),
                      Divider(),
                      Row(
                        children: [
                          Flexible(
                              flex: 1,
                              child: Container(
                                width: double.infinity,
                                height: 50,
                                decoration: BoxDecoration(
                                  color: Colors.white,
                                  borderRadius: BorderRadius.only(
                                    bottomLeft: Radius.circular(8),
                                  ),
                                ),
                                alignment: Alignment.center,
                                child: Text("${message['product26']}",style: TextStyle(color: HexColor("#999999"),fontSize: 16),),
                              ).addGesture(() {
                                Navigator.of(context).pop();
                              })
                          ),
                          ColoredBox(color: HexColor("#E8E8E8"),
                            child: SizedBox(width: 1,height: 50,),
                          ),
                          Flexible(
                              flex: 1,
                              child: Container(
                                width: double.infinity,
                                height: 50,
                                decoration: BoxDecoration(
                                  color: Colors.white,
                                  borderRadius: BorderRadius.only(
                                    bottomRight: Radius.circular(8),
                                  ),
                                ),
                                alignment: Alignment.center,
                                child: Text("${message['product27']}",style: TextStyle(color: AppColors.primaryColor,fontSize: 16),),
                              ).addGesture(() {
                                if(_controller1.text == ""){
                                  ToastUtils.show("${message['enterPhone']}", context);
                                  return;
                                }
                                if(_controller2.text == ""){
                                  ToastUtils.show("${message['enterPhone']}", context);
                                  return;
                                }
                                LoadingApi.func(UserRepository().bindRealName(_controller1.text, _controller2.text), context,complete: (v){
                                  ToastUtils.show("$v", context);
                                  UserPhoneCache().save(_controller2.text);
                                  Navigator.of(context).pop();
                                },error: (e){
                                  ToastUtils.show("$e", context);
                                  Navigator.of(context).pop();
                                });
                              })
                          )
                        ],
                      )
                    ],
                  )
              )
          ),
        ),
        onWillPop: () async {
          return true;
        },
      ),
    );
  }
}